package net.mikoo.seals.manage.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.biz.service.PartyServices;
import net.mikoo.seals.biz.service.WorkflowService;
import net.mikoo.seals.common.WebContext;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.manage.web.common.BaseController;
import net.mikoo.seals.model.Party;
import net.mikoo.seals.model.PartyQuery;
import net.mikoo.seals.model.Workflow;
import net.mikoo.seals.model.WorkflowQuery;

@RequestMapping("/workflow")
@Controller
public class WorkflowController extends BaseController {

	private static Logger logger = LoggerFactory.getLogger(WorkflowController.class);
	
	@Autowired
	private WorkflowService workflowService;
	
	@Autowired
	private PartyServices partyServices;
	
	
	
	
	
	
	
	
	@RequestMapping("/update")
	public String update(Long workflowId, Model model) {

		if (workflowId == null) {
			throw new ParametersException("workflowId");
		}

		Workflow workflow = workflowService.getInfoById(workflowId);

		model.addAttribute("workflow", workflow);
		return "/workflow/update";

	}
	
	@RequestMapping("/modify")
	@ResponseBody
	public Map<String, Object> modify(Workflow workflow, HttpServletRequest request){
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			workflow.setCompanyId(companyId);
			
			workflowService.modify(workflow);
			return result(SUCCESS, "设置成功");
		}catch(ParametersException pe) {
			logger.error("", pe);
			return result(ERROR, pe.getMessage());
		}catch(Exception e) {
			logger.error("系统错误", e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	
	
	@RequestMapping("/staffs")
	public String staffs(HttpServletRequest request,PartyQuery query,Model model) {
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		PageModel<Party> pm = partyServices.getPageByQuery(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		return "/workflow/staffs";
	}
	
	
	@RequestMapping("/test")
	public String test(HttpServletRequest request,PartyQuery query,Model model) {
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		PageModel<Party> pm = partyServices.getPageByQuery(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		return "/workflow/test";
	}
	
	@RequestMapping("/parent")
	public String parent(HttpServletRequest request,PartyQuery query,Model model) {
		
		Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
		query.setCompanyId(companyId);
		PageModel<Party> pm = partyServices.getPageByQuery(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		return "/workflow/parent";
	}
	
	
	@RequestMapping("/publish")
	@ResponseBody
	public Map<String,Object> publish(Workflow workflow,HttpServletRequest request,Model model) {
		
		try {
			
			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			workflow.setCompanyId(companyId);
			workflowService.publish(workflow);
			
			return result(SUCCESS, MSG_SUCCESS);
			
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, MSG_SYSTEM_ERROR);
		}
	}
	
	
//	@RequestMapping("/switchEnable")
//	@ResponseBody
//	public Map<String, Object> switchEnable(Long workflowId, Model model, HttpServletRequest request){
//		try {
//			if(workflowId == null) {
//				throw new ParametersException("参数错误");
//			}
//			
//			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
//			if(companyId == null) {
//				throw new ParametersException("获取当前用户的公司信息失败");
//			}
//			
//			workflowService.switchEnable(workflowId, companyId);
//			return result(SUCCESS, "修改成功");
//		}catch(ParametersException pe) {
//			logger.error("", pe);
//			return result(ERROR, pe.getMessage());
//		}catch(Exception e) {
//			logger.error("", e);
//			return result(ERROR, MSG_SYSTEM_ERROR);
//		}
//		
//	}
	
//	@RequestMapping("/setting")
//	public String setting(Long workflowId, Model model) {
//		
//		if(workflowId == null) {
//			throw new ParametersException("workflowId");
//		}
//		Workflow workflow = workflowService.getInfoById(workflowId);
//		if(workflow != null) {
//			model.addAttribute("workflow", workflow);
//		}
//		
//		List<WorkflowSetting> settingList = workflowService.getSetting(workflowId);
//		if(settingList != null) {
//			model.addAttribute("settingList", settingList);
//		}
//		
//		return "/workflow/setting";
//	}
	
//	@RequestMapping("setting/submit")
//	@ResponseBody
//	public Map<String, Object> settingSubmit(Long workflowId, String ids, HttpServletRequest request){
//		try {
//			if(workflowId == null) {
//				throw new ParametersException("参数错误");
//			}
//			
//			if(StringUtils.isBlank(ids)) {
//				throw new ParametersException("审核人员不能设置为空");
//			}
//			
//			List<String> idList = new ArrayList<String>();
//			for(String id : ids.split(",")) {
//				if(StringUtils.isNotBlank(id)) {
//					idList.add(id);
//				}
//			}
//			
//			if(idList.isEmpty()) {
//				throw new ParametersException("审核人员存在无效人员");
//			}
//			
//			Long companyId = getCompanyId(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
//			if(companyId == null) {
//				throw new ParametersException("获取当前用户的公司信息失败");
//			}
//			
//			workflowService.setSettings(idList, workflowId, companyId);
//			return result(SUCCESS, "设置成功");
//		}catch(ParametersException pe) {
//			logger.error("", pe);
//			return result(ERROR, pe.getMessage());
//		}catch(Exception e) {
//			logger.error("", e);
//			return result(ERROR, MSG_SYSTEM_ERROR);
//		}
//	}
//	
	
	
	
	
}
